/**
 * @author admin
 *14.最长公共前缀
 * // 纵向扫描：以第一个字符串为基准，从第一个字母开始匹配，遇到不匹配的，直接返回空，否则叠加字符串
 */
public class LeeCode14 {

    public String longestCommonPrefix(String[] strs) {

        if(strs.length == 0 || strs == null){
            return "";
        }

        int length = strs[0].length();
        int count = strs.length;

        for (int i = 0; i < length; i++) {
            char c = strs[0].charAt(i);
            //当前公共前缀
            for (int j = 1; j < count; j++) {
                if (i == strs[j].length() || strs[j].charAt(i) != c) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }


    public static void main(String[] args) {
        String[] strs = new String[]{"flower", "flow", "flight"};

        LeeCode14 leeCode14 = new LeeCode14();
        String s = leeCode14.longestCommonPrefix(strs);
        System.out.println("最长公共前缀为：" + s);
    }
}
